#region namespaces
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using BoOnce.Core.Input;
using BoOnce.Core.Helpers;
#endregion

namespace BoOnce.Core.Components
{
    public class InputComponent : GameComponent
    {
        public enum Buttons { Left, Right, Up, Down, Action1, Action2 };

        private KeyboardInput kb;
        private GamePadInput gp;

        public InputComponent(Game game, String keyboardConfig)
            : base(game)
        {
            gp = new GamePadInput();
            kb = new KeyboardInput(keyboardConfig);
        }

        public override void Initialize()
        {
            Logger.WriteInfo("InputComponent[#" + this.UpdateOrder + "] init");
        }

        public override void Update(GameTime gameTime)
        {
            gp.Update();
            kb.Update();
        }

#if DEBUG
        public Boolean IsNewPressDebug(Keys k)
        {
            return kb.IsNewKeyPress(PlayerIndex.One, k);
        }
#endif

        public Boolean IsPress(PlayerIndex playerIndex, Buttons button)
        {
            switch (button)
            {
                case Buttons.Left:
                    return kb.IsPress(playerIndex,kb.kbConfig.left);

                case Buttons.Right:
                    return kb.IsPress(playerIndex, kb.kbConfig.right);

                case Buttons.Up:
                    return kb.IsPress(playerIndex, kb.kbConfig.up);

                case Buttons.Down:
                    return kb.IsPress(playerIndex, kb.kbConfig.down);

                case Buttons.Action1:
                    return kb.IsPress(playerIndex, kb.kbConfig.action1);

                case Buttons.Action2:
                    return kb.IsPress(playerIndex, kb.kbConfig.action2);

                default:
                    Logger.WriteWarning(this,"Button not supported by Input.IsPress: " + button);
                    return false;
            } 
        }

        public Boolean IsNewPress(PlayerIndex playerIndex, Buttons button)
        {
            switch (button)
            {
                case Buttons.Left:
                    return kb.IsNewKeyPress(playerIndex, kb.kbConfig.left);

                case Buttons.Right:
                    return kb.IsNewKeyPress(playerIndex, kb.kbConfig.right);

                case Buttons.Up:
                    return kb.IsNewKeyPress(playerIndex, kb.kbConfig.up);

                case Buttons.Down:
                    return kb.IsNewKeyPress(playerIndex, kb.kbConfig.down);

                case Buttons.Action1:
                    return kb.IsNewKeyPress(playerIndex, kb.kbConfig.action1);

                case Buttons.Action2:
                    return kb.IsNewKeyPress(playerIndex, kb.kbConfig.action2);

                default:
                    Logger.WriteWarning(this,"Button not supported by Input.IsNewPress: " + button);
                    return false;
            }
        }
    }
}
